package vashion.azeroth.common.http;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * @author Loki 2017.05.23 获得http接口的内容数据
 *
 */
public class HttpContentGetter {

	/**
	 * @param url
	 * @param nameValueMap
	 * @param headerMap
	 * @return
	 */
	public static String getContentString(String url, Map<String, String> nameValueMap, Map<String, String> headerMap) {
		String result = null;
		CloseableHttpClient client = HttpClients.createDefault();
		StringBuffer strBuf = new StringBuffer(url);
		StringBuffer nameValueBuf = new StringBuffer();
		if (null != nameValueMap) {
			for (Entry<String, String> nameValueEntry : nameValueMap.entrySet()) {
				nameValueBuf.append("&").append(nameValueEntry.getKey()).append("=").append(nameValueEntry.getValue());
			}
			strBuf.append("?").append(nameValueBuf.toString().substring(1));
		}
		HttpGet get = new HttpGet(strBuf.toString());

		// 设置Header
		if (null != headerMap) {
			for (Entry<String, String> headerEntry : headerMap.entrySet()) {
				get.setHeader(headerEntry.getKey(), headerEntry.getValue());
			}
		}

		try {
			CloseableHttpResponse response = client.execute(get);
			if (response != null && response.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(response.getEntity());
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (client != null) {
				try {
					client.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/**
	 * @param url
	 * @param nameValueMap
	 * @param headerMap
	 * @return
	 */
	public static String postContentString(String url, Map<String, String> nameValueMap,
			Map<String, String> headerMap) {

		String result = null;
		CloseableHttpClient client = HttpClients.createDefault();
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		if (null != nameValueMap) {
			for (Entry<String, String> nameValueEntry : nameValueMap.entrySet()) {
				nvps.add(new BasicNameValuePair(nameValueEntry.getKey(), nameValueEntry.getValue()));
			}
		}
		HttpPost post = new HttpPost(url);

		// 设置Header
		if (null != headerMap) {
			for (Entry<String, String> headerEntry : headerMap.entrySet()) {
				post.setHeader(headerEntry.getKey(), headerEntry.getValue());
			}
		}

		try {
			post.setEntity(new UrlEncodedFormEntity(nvps));
			CloseableHttpResponse response = client.execute(post);
			if (response != null && response.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(response.getEntity());
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (client != null) {
				try {
					client.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
}
